package game.view
{
	import game.constHlp.PoolType;
	import game.constHlp.RoleActions;
	import game.movie.EnemyMovie;
	import game.movie.RoleMovie;
	
	import laya.display.Sprite;
	import laya.events.Event;
	import laya.maths.Rectangle;
	import laya.net.Loader;
	import laya.resource.Texture;
	import laya.utils.Browser;
	import laya.utils.Pool;
	
	/**
	 * 整个游戏的容器
	 *@date 2017-2-20 下午4:30:48
	 *@author zhouhonghui
	 */
	public class AirWar extends Sprite
	{
		/**创建敌机的时间间隔**/
		private const ENEMY_CD:int = 1500;
		
		private var _enemyEndTime:Number=0;
		private var _enemyLock:Boolean;
		private var _bgTexture:Texture;
		private var _bg1:Sprite;
		private var _bg2:Sprite;

		private var _gameRole:RoleMovie;
		private var _roleBullets:Array;
		private var _enemyBullets:Array;
		private var _enemys:Array;
		
		
		public function AirWar()
		{
			super();
			initUI();
			_roleBullets = [];
			_enemyBullets = [];
			_enemys = [];
			this.on(Event.DISPLAY,this,onAddToStage);
			this.on(Event.UNDISPLAY,this,onRemoveFromStage);
		}
		private function initUI():void
		{
			_bgTexture = Loader.getRes("res/background.png");			
			_bg1 = new Sprite();
			_bg1.graphics.drawTexture(_bgTexture);
			addChild(_bg1);
			
		
			
			_bg2 = new Sprite();
			_bg2.graphics.drawTexture(_bgTexture);
			addChild(_bg2);
			_bg2.y = _bgTexture.height;			
			
			_gameRole = new RoleMovie();
			//ani.autoPlay = true;
			//ani.source = "war/hero_fly1.png,war/hero_fly2.png";
			addChild(_gameRole);		
			
		}
		private function onAddToStage(e:*=null):void
		{
			this.stage.on(Event.MOUSE_MOVE,this,onMouseMove);
			this.stage.on(Event.MOUSE_DOWN,this,onMouseDown);
			Laya.timer.loop(120,this,onLoopUpdate);
			onMouseMove();
			
		}
		private function onRemoveFromStage(e:*=null):void
		{
			this.stage.off(Event.MOUSE_MOVE,this,onMouseMove);
			this.stage.off(Event.MOUSE_DOWN,this,onMouseDown);
			Laya.timer.clear(this,onLoopUpdate);
		}
		
		private function onMouseMove(e:*=null):void
		{
			_gameRole.pos(stage.mouseX-49,stage.mouseY-62);
			
		}
		
		private function onMouseDown(e:*=null):void
		{
			if(_gameRole)
			{
				var b:Bullet = _gameRole.shoot();
				addChild(b);
				_roleBullets.push(b);
			}
			
		}
		
		private function onLoopUpdate():void
		{
			scrollBg(); //滚动背景
			//创建敌机
			createEnemy();
			checkHit();
			checkEnemys(_enemys);
			checkBullets(_roleBullets);
		}
		private function scrollBg():void
		{
			var speed:int = 20;
			_bg1.y-=speed;
			if(_bg1.y<=-(_bgTexture.height-speed))
			{
				_bg1.y = _bgTexture.height-Math.abs(_bg2.y);
			}
			_bg2.y-=speed;
			if(_bg2.y<=-(_bgTexture.height-speed))
			{
				_bg2.y = _bgTexture.height-Math.abs(_bg1.y);
			}
		}
		private function createEnemy():void
		{
			// TODO Auto Generated method stub
			var now:Number = Browser.now();			
			if(now>=_enemyEndTime && !_enemyLock)
			{
				_enemyEndTime = now+ENEMY_CD;
				var e:EnemyMovie = new EnemyMovie();//Pool.getItemByClass(PoolType.ENEMY,EnemyMovie);
				e.pos(Math.random()*(Laya.stage.width-180),0);
				e.eType = parseInt(""+(1+Math.random()*2));
				e.action = RoleActions.FLY;						
				addChild(e);
				_enemys.push(e);
			}
		}
				
		private function checkBullets(bullets:Array):void
		{
			for (var i:int = 0; i < bullets.length; i++) 
			{
				var b:Bullet = bullets[i];
				if(b.isOut()) //子弹已经出了边界或者碰撞
				{
					recoverBullet(b,bullets,i);
					i--;
				}else
				{
					b.y-=b.speed;
				}				
			}			
		}
		
		private function checkEnemys(enemys:Array):void
		{
			for (var i:int = 0; i < enemys.length; i++) 
			{
				var e:EnemyMovie = enemys[i];				
				if(e.isOut()) //敌人已经出了边界或者碰撞
				{					
					trace("敌机出边界..");
					recoverEnemy(e,enemys,i);		
					i--;
				}else
				{
					e.y+=e.speed;
				}				
			}			
		}
		
		private function checkHit():void
		{
			var e:EnemyMovie;
			var b:Bullet;
			var isGameOver:Boolean = false;
			for (var i:int = 0; i < _enemys.length; i++) 
			{
				e = _enemys[i];				
				var r:Rectangle = e.getBounds();//获取敌机的尺寸 这个方法会影响性能
				var dx:Number;
				var dy:Number;
				var flag:Boolean = false;
				for (var j:int = 0; j < _roleBullets.length; j++) //检测敌机和子弹的碰撞
				{
					b = _roleBullets[j];
					dx = Math.abs(b.x - e.x);
					dy = Math.abs(b.y-e.y);					
					if(dx<=r.width && dy<=r.height)
					{
						//trace("碰撞了.... r.height,d",r.height,d);
						trace("敌机碰撞子弹..");
						flag = true;
						recoverBullet(b,_roleBullets,j);				
						recoverEnemy(e,_enemys,i);		
						break;
					}
				}	
				
				//检测敌机和主角的碰撞
				if(!flag)
				{
					dx = Math.abs(_gameRole.x - e.x);
					dy = Math.abs(_gameRole.y-e.y);			
					
					if(dx<=r.width && dy<=r.height) //  r.intersects(_gameRole.getBounds())
					{
						isGameOver = true;															
						break;
					}
				}
				
			}
			if(isGameOver)
			{
				gameOver();				
			}
			
		}
		private function gameOver():void
		{
			trace("gameOver....");
			onRemoveFromStage();
			if(_gameRole.parent)
			{
				_gameRole.parent.removeChild(_gameRole);
				_gameRole.stop();
				_gameRole.destroy();
				_gameRole = null;
			}
			
			while(_roleBullets.length>0) //主角死了 清空子弹
			{
				recoverBullet(_roleBullets[0],_roleBullets,0);
			}				
			/*while(_enemys.length>0){
				
				recoverEnemy(_enemys[0],_enemys,0);
			}*/
		}	
		private function recoverBullet(b:Bullet,arr:Array,i:int):void
		{
			if(b.parent)
			{
				b.parent.removeChild(b);
			}
			var b2:Bullet = arr[i];
			if(b==b2)
			{
				arr.splice(i,1);
			}
			Pool.recover(PoolType.BULLET,b);	
		}
		private function recoverEnemy(en:EnemyMovie,arr:Array,i:int):void
		{
			_enemyLock = true;
			var en2:EnemyMovie = arr[i];
			if(en==en2)
			{
				arr.splice(i,1);
			}
			en.isDeath = true;
			en.action = RoleActions.DOWN;
			en.on(Event.COMPLETE,this,onDownEnemyDownComplete,[en]);				
		}
		
		private function onDownEnemyDownComplete(e:EnemyMovie):void
		{
			if(e.parent)
			{
				e.parent.removeChild(e);
			}
			//Pool.recover(PoolType.ENEMY,e);
			_enemyLock = false;
		}
		
		
		
	}
}